<template>
	<div class="back_contain_n">
		<!-- 头部搜索 -->
		<div class="Search_Top_Part">
			<div class="search_list">
				<div>
					<div class="input_flex">
						<el-input clearable v-model="queryForm.customer" placeholder="客户"></el-input>
					</div>
					<div class="input_flex">
						<el-select clearable v-model="queryForm.itemId" placeholder="品类">
							<el-option v-for="(item,index) in itemList" :key="index" :label="item" :value="item" />
						</el-select>
					</div>
					<div class="input_flex">
						<el-select clearable v-model="queryForm.gradeValue" placeholder="规格">
							<el-option v-for="(item,index) in gradeList" :key="index" :label="item" :value="item" />
						</el-select>
					</div>
					<div class="input_flex daterange">
						<el-date-picker v-model="time" type="daterange" :picker-options="pickerOptions"
							range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
						</el-date-picker>
					</div>
				</div>
			</div>
			<div class="search_bt">
				<span class="zll-search" @click="getList">搜索</span>
				<span class="zll-search-reset" @click="searchReset()">重置</span>
			</div>
		</div>
		<div class="Search_Top_Input">
			<div class="addNew">
				<span @click="edit({id:''},'add')" v-if="hasRole('delivery:order:in')"><i
						class="el-icon-circle-plus-outline"></i> 订单录入</span>
			</div>
		</div>
		<!-- table -->
		<sys-table class="m_table" ref="systable" :isMultipleSelection="false" :tableData="tableData"
			:tableLoading="tableLoading" :tableHeader="tableHeader" :scopeWidth="120" :backCount="backCount"
			@getBackData="getList">
			<template slot-scope="scope" slot="operate">
				<el-button @click="edit(scope.row,'see')" type="text" size="small"
					v-if="hasRole('delivery:order:see')">查看</el-button>
				<el-button @click="edit(scope.row,'edit')" v-if="hasRole('delivery:order:edit')" type="text"
					size="small">编辑</el-button>
				<el-button type="text" size="small" @click="deleteRow(scope.row)"
					v-if="hasRole('delivery:order:del')">删除</el-button>
			</template>
		</sys-table>
		<!---- 弹框
            订单录入 add 
            查看+编辑 detail 
        -------->
		<div class="zll-dialog">
			<popout :title="'订单 · ' + (type == 'edit'?'编辑':(type =='see'?'查看':'录入'))" :visible.sync="addDialog"
				v-if="addDialog" :class="type == 'add'?'max_size':'largeSize'">
				<add ref="add" slot="content" v-if="type == 'add'" :type="type" :selectItems="itemList"
					:editData="editData"></add>
				<detail ref="add" slot="content" v-else :editData="editData" :type="type" :selectItems="itemList">
				</detail>
				<template slot="bottom">
					<p class="zll-botton Info" @click="addDialog = false">关 闭</p>
					<p class="zll-botton" v-if="type !== 'see'" @click="()=>{this.$refs.add.submitForm('addForm')}">提
						交</p>
				</template>
			</popout>
		</div>
	</div>
</template>

<script>
	import Add from './add'
	import Detail from './detail'
	import { formatDate } from "@/plugins/formatDate";
	import { getStore } from "@/plugins/localStore";
	export default {
		data() {
			return {
				tableLoading: true, //table刷新
				type: '',
				editData: {},

				queryForm: {
					gradeValue: '',
					itemId: '',
					customer: '',
					orderStatus: '1',
				},
				backCount: 0,
				tableData: [],
				tableHeader: [],
				addDialog: false,

				gradeList: [],
				itemList: ['单支连排', '多支', '伸缩', 'U型'],
				time: [],
				pickerOptions: {
					onPick: ({ minDate }) => {
						this.minDate = minDate
					},
					disabledDate: (time) => {
						if (this.minDate) {
							const oneYear = 365 * 24 * 60 * 60 * 1000
							return time.getTime() < this.minDate.getTime() ||
								time.getTime() > this.minDate.getTime() + oneYear
						}
						return false
					}
				}
			}
		},
		methods: {

			getSysItemList() {
				let self = this;
				self.itemList = [];
				self.$http({
					url: "/jinl/sysItem/getMgItemSelect",
					method: "post",
					data: JSON.stringify({ types: ['99'] }),
					dataType: 'json',
					contentType: "application/json",
				}).then(resp => {
					if (resp.success) {
						self.itemList = resp.result;
					}
				});
			},

			getOrderGrades() {
				let self = this;
				self.gradeList = [];
				self.$http({
					url: "/jinl/order/getOrderGrades",
					method: "post",
				}).then(resp => {
					if (resp.success) {
						self.gradeList = resp.result;
					}
				});
			},

			deleteRow(rowData) {
				let self = this;
				self.$confirm('是否确认删除？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					self.$http({
						url: '/jinl/order/deleteOrders',
						method: "post",
						params: { id: rowData.id }
					}).then(resp => {
						if (resp.success) {
							self.getList();
							self.$message.success('删除成功！');
							self.$eventBus.$emit("getOrderNum");
							return
						}
						self.$message.error(resp.message);
					});
				});
			},

			getList(startIndex, pageRow) {
				let self = this;
				self.tableLoading = true;
				if (this.time && this.time.length == 2) {
					//date类型
					self.queryForm.startTime = formatDate(new Date(self.time[0]), 'yyyy-MM-dd')
					self.queryForm.endTime = formatDate(new Date(self.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
				} else {
					self.queryForm.startTime = ''
					self.queryForm.endTime = ''
				}
				let pageInfo = self.$refs.systable.getPageInfo(startIndex);
				if (pageInfo.pageNum == 0 || startIndex == 0) {
					self.$refs.systable.resetPage();
				}
				self.queryForm.startIndex = pageInfo.pageNum;
				self.queryForm.totalPage = pageInfo.pageSize;
				let param = Object.assign({}, self.queryForm);
				self.$http({
					url: "/jinl/order/getOrderList",
					method: "post",
					data: JSON.stringify(param),
					dataType: 'json',
					contentType: "application/json",
				}).then(resp => {
					if (resp.success) {
						let userId = getStore().id;
						self.tableLoading = false;
						for (let data of resp.result) {
							// if(data.createUserId != userId){
							// 	data.price = '*****';
							// 	data.totalPrice = '******';
							// }
						}
						self.tableData = resp.result;
						self.tableHeader = [
							{ "columnValue": "orderNum", "columnName": "订单号", width: 120, overFlow: true },
							{ "columnValue": "orderType", "columnName": "类型" },
							{ "columnValue": "customer", "columnName": "客户", width: 200, overFlow: true },
							{ "columnValue": "itemTypeId", "columnName": "品类" },
							{ "columnValue": "productName", "columnName": "产品名称", width: 180, overFlow: true },
							{ "columnValue": "gradeValue", "columnName": "规格", width: 180, overFlow: true },
							{ "columnValue": "thNumber", "columnName": "单件万支" },
							{ "columnValue": "peieceNum", "columnName": "件数" },
							{ "columnValue": "totalNum", "columnName": "合计(万支)" },
							// {"columnValue": "price", "columnName": "单价"},
							// {"columnValue": "totalPrice", "columnName": "金额"},
							{ "columnValue": "orderStatusCn", "columnName": "状态" },
						];
						if (resp.result.length > 0) {
							self.backCount = resp.result[0].count;
						} else {
							self.backCount = 0;
						}
					}
				});

			},


			edit(val, type) {
				let self = this;
				self.addDialog = true;
				self.type = type;
				self.$nextTick(() => {
					self.itemList = Object.assign({}, self.itemList);
					self.editData = Object.assign({}, val);
				});
			},
			searchReset() { //重置搜索
				this.time = [];
				this.queryForm = {
					gradeValue: '',
					itemId: '',
					customer: '',
					orderStatus: '1',
				};
				this.getList();
			},

			init(val) {
				this.searchReset();
			},
		},
		mounted() {
			let self = this;
			// self.getSysItemList();
			self.getOrderGrades();
			self.$eventBus.$on("updateOrderList", function () {
				self.addDialog = false;
				self.getList();
				self.getOrderGrades()
			})
		},
		components: {
			Add, Detail
		},
	}
</script>

<style lang="scss" scoped>
	@import "@/style/SearchTop.scss";
</style>